Page({
  data: {
    selectedBoat: {
      id: '',
      boatNumber: ''
    },
    location: '',
    fishingTime: '',
    quantity: '',
    species: ''
  },

  onLoad() {
    this.loadBoats();
  },

  loadBoats() {
    wx.request({
      url: 'https://api.example.com/boats',
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            boats: res.data
          });
        } else {
          wx.showToast({
            title: '获取渔船列表失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  selectBoat() {
    const { boats } = this.data;
    wx.showActionSheet({
      itemList: boats.map(boat => boat.boatNumber),
      success: (res) => {
        const selectedBoat = boats[res.tapIndex];
        this.setData({
          selectedBoat
        });
      }
    });
  },

  onLocationChange(e) {
    this.setData({
      location: e.detail.value
    });
  },

  selectTime() {
    wx.showDatePicker({
      current: this.data.fishingTime,
      start: '2000-01-01',
      end: '2100-12-31',
      success: (res) => {
        this.setData({
          fishingTime: res.date
        });
      }
    });
  },

  onQuantityChange(e) {
    this.setData({
      quantity: e.detail.value
    });
  },

  onSpeciesChange(e) {
    this.setData({
      species: e.detail.value
    });
  },

  submit() {
    const { selectedBoat, location, fishingTime, quantity, species } = this.data;
    if (!selectedBoat.id || !location || !fishingTime || !quantity || !species) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: 'https://api.example.com/fishing-records',
      method: 'POST',
      data: {
        boatId: selectedBoat.id,
        location,
        fishingTime,
        quantity,
        species
      },
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'success'
          });
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  goBack() {
    wx.navigateBack();
  }
}); 